<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
            v-model="queryParams.userId"
            placeholder="请输入借阅用户的 ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input
            v-model="queryParams.userName"
            placeholder="请输入借阅用户的用户名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="档案ID" prop="archiveId">
        <el-input
            v-model="queryParams.archiveId"
            placeholder="请输入借阅的档案ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="申请时间" prop="applyTime">-->
<!--        <el-date-picker clearable-->
<!--                        v-model="queryParams.applyTime"-->
<!--                        type="date"-->
<!--                        value-format="YYYY-MM-DD"-->
<!--                        placeholder="请选择申请时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="借阅开始日期" prop="startDate" label-width="100">
        <el-date-picker clearable
                        v-model="queryParams.startDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择借阅开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="借阅结束日期" prop="endDate" label-width="100">
        <el-date-picker clearable
                        v-model="queryParams.endDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择借阅结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="借阅状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择借阅状态" clearable>
          <el-option
              v-for="dict in iams_borrow_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="实际借出日期" prop="borrowDate" label-width="100">
        <el-date-picker clearable
                        v-model="queryParams.borrowDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择实际借出日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="实际归还日期" prop="returnDate" label-width="100">
        <el-date-picker clearable
                        v-model="queryParams.returnDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择实际归还日期">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="逾期天数" prop="overdueDays">-->
<!--        <el-input-->
<!--            v-model="queryParams.overdueDays"-->
<!--            placeholder="请输入逾期天数"-->
<!--            clearable-->
<!--            @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['manage:record:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="success"-->
<!--            plain-->
<!--            icon="Edit"-->
<!--            :disabled="single"-->
<!--            @click="handleUpdate"-->
<!--            v-hasPermi="['manage:record:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="danger"-->
<!--            plain-->
<!--            icon="Delete"-->
<!--            :disabled="multiple"-->
<!--            @click="handleDelete"-->
<!--            v-hasPermi="['manage:record:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['manage:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="借阅记录ID" align="center" prop="id" />-->
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="档案ID" align="center" prop="archiveId" />
<!--      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="借阅开始日期" align="center" prop="startDate" width="150">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="借阅结束日期" align="center" prop="endDate" width="150">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="借阅目的" align="center" prop="purpose" width="200"/>
      <el-table-column label="借阅状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="iams_borrow_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="实际借出日期" align="center" prop="borrowDate" width="150">
        <template #default="scope">
          <span>{{ parseTime(scope.row.borrowDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际归还日期" align="center" prop="returnDate" width="150">
        <template #default="scope">
          <span>{{ parseTime(scope.row.returnDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="逾期天数" align="center" prop="overdueDays" />-->
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="流程名" align="center" prop="processName" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:record:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:record:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改借阅记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="recordRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入借阅用户的 ID" />
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入借阅用户的用户名" />
        </el-form-item>
        <el-form-item label="档案ID" prop="archiveId">
          <el-input v-model="form.archiveId" placeholder="请输入借阅的档案ID" :disabled="isInputDisabled"/>
        </el-form-item>
<!--        <el-form-item label="申请时间" prop="applyTime">-->
<!--          <el-date-picker clearable-->
<!--                          v-model="form.applyTime"-->
<!--                          type="date"-->
<!--                          value-format="YYYY-MM-DD"-->
<!--                          placeholder="请选择申请时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker clearable
                          v-model="form.startDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择借阅开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker clearable
                          v-model="form.endDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择借阅结束日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="启动流程" prop="processName">
        <el-select v-model="form.processName" placeholder="请选择待启动流程" clearable>
          <el-option
              v-for="dict in Modeler"
              :label="dict.name"
              :value="dict.key"
          />
        </el-select>
        </el-form-item>

        <el-form-item label="借阅目的" prop="purpose">
          <el-input v-model="form.purpose" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="借阅状态" prop="status">-->
<!--          <el-select v-model="form.status" placeholder="请选择借阅状态">-->
<!--            <el-option-->
<!--                v-for="dict in iams_borrow_status"-->
<!--                :key="dict.value"-->
<!--                :label="dict.label"-->
<!--                :value="dict.value"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="实际借出日期" prop="borrowDate">-->
<!--          <el-date-picker clearable-->
<!--                          v-model="form.borrowDate"-->
<!--                          type="date"-->
<!--                          value-format="YYYY-MM-DD"-->
<!--                          placeholder="请选择实际借出日期">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="实际归还日期" prop="returnDate">-->
<!--          <el-date-picker clearable-->
<!--                          v-model="form.returnDate"-->
<!--                          type="date"-->
<!--                          value-format="YYYY-MM-DD"-->
<!--                          placeholder="请选择实际归还日期">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="逾期天数" prop="overdueDays">-->
<!--          <el-input v-model="form.overdueDays" placeholder="请输入逾期天数" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Record">
import {listRecord, getRecord, delRecord, addRecord, updateRecord} from "@/api/manage/borrowrecord";
import {listDefinition} from "@/api/activiti/definition"
import {ref, onMounted} from "vue";
import { useRoute } from 'vue-router';

const {proxy} = getCurrentInstance();
const {iams_borrow_status} = proxy.useDict('iams_borrow_status');
const isInputDisabled = ref(false)

const route = useRoute();
const queryParams_router = ref({
  archiveId: route.query.id,
  archiveName: route.query.name,
});


const queryParams_activitiList = reactive({
  pageNum: 1,
  pageSize: 5,
  id: null,
  rev: null,
  name: null,
  type: null,
});


const recordList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    userName: null,
    archiveId: null,
    applyTime: null,
    startDate: null,
    endDate: null,
    purpose: null,
    status: null,
    borrowDate: null,
    returnDate: null,
    overdueDays: null,
    processName: null
  },
  rules: {
    userId: [
      {required: true, message: "借阅用户的 ID不能为空", trigger: "blur"}
    ],
    userName: [
      {required: true, message: "借阅用户的用户名不能为空", trigger: "blur"}
    ],
    archiveId: [
      {required: true, message: "借阅的档案ID不能为空", trigger: "blur"}
    ],
    startDate: [
      {required: true, message: "借阅开始日期不能为空", trigger: "blur"}
    ],
    endDate: [
      {required: true, message: "借阅结束日期不能为空", trigger: "blur"}
    ],
    processName: [
      {required: true, message: "流程名称不能为空", trigger: "blur"}
    ],
    purpose: [
      {required: true, message: "借阅目的不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询借阅记录列表 */
function getList() {
  loading.value = true;
  listRecord(queryParams.value).then(response => {
    recordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
  proxy.$router.push({ path: "/manage/record"});
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    userName: null,
    archiveId: null,
    applyTime: null,
    startDate: null,
    endDate: null,
    purpose: null,
    status: null,
    borrowDate: null,
    returnDate: null,
    overdueDays: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    remark: null,
    processName: null
  };
  proxy.resetForm("recordRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}



const Modeler = ref([]);
const total_activiti = ref(0);


/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加借阅记录";
  isInputDisabled.value = false;

  listDefinition(queryParams_activitiList).then(response => {
    Modeler.value = response.rows;
    total_activiti.value = response.total;
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getRecord(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改借阅记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["recordRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除借阅记录编号为"' + _ids + '"的数据项？').then(function () {
    return delRecord(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/record/export', {
    ...queryParams.value
  }, `record_${new Date().getTime()}.xlsx`)
}

// 在组件作用域内声明状态标记
const hasHandledInitialParams = ref(false)

// 带参数时的附加操作
const handleParamsOperations = () => {
  open.value = true
  form.value.archiveId = route.query.record_id // 使用正确的参数名
  isInputDisabled.value = true
  listDefinition(queryParams_activitiList).then(response => {
    Modeler.value = response.rows
    total_activiti.value = response.total
  })
}

onMounted(() => {

  getList()

  // 处理直接通过URL带参数访问的情况
  if (route.query.record_id && !hasHandledInitialParams.value) {
    handleParamsOperations()
    hasHandledInitialParams.value = true
  }
})

watch(
    () => route.query.record_id, // 根据实际参数名调整
    (newId) => {
      if (route.path === "/manage/record") {
        // 首次处理参数逻辑
        if (newId && !hasHandledInitialParams.value) {
          handleParamsOperations()
          hasHandledInitialParams.value = true
        }
        // 参数清除时关闭弹窗
        if (!newId && open.value) {
          open.value = false
        }
      }
    },
    { immediate: true }
)

import { onBeforeUnmount } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'

onBeforeRouteLeave((to, from) => {
  if (from.path === "/manage/record") {
    // 保留状态用于同路由切换
    // 仅在完全离开路由时重置
    if (to.path !== "/manage/record") {
      hasHandledInitialParams.value = false
      open.value = false
    }
  }
})

onBeforeUnmount(() => {
  hasHandledInitialParams.value = false
  open.value = false
})

</script>
